package com.hkts.management.unattended.utils;

import com.hkts.management.unattended.constants.UnattendedConstants;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.io.UnsupportedEncodingException;
import java.util.Base64;


/**
 * @ClassNameEncryptionUtils
 * @Description
 * @Author yck
 * @Date2021-09-13 10:16
 * @Version V1.0
 **/
public class EncryptionUtils {

    public static String Encrypt(String sSrc) throws Exception {
        sSrc= EncryptionUtils.base64Code(sSrc);
        byte[] raw = UnattendedConstants.UNATTENDED_KEY.getBytes("utf-8");
        SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");//"算法/模式/补码方式"
        cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
        byte[] encrypted = cipher.doFinal(sSrc.getBytes("utf-8"));
        return new BASE64Encoder().encode(encrypted);//此处使用BASE64做转码功能，同时能起到2次加密的作用。
    }

    // 解密
    public static String Decrypt(String sSrc) throws Exception {
        try {
            byte[] raw = UnattendedConstants.UNATTENDED_KEY.getBytes("utf-8");
            SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
            cipher.init(Cipher.DECRYPT_MODE, skeySpec);
            byte[] encrypted1 = new BASE64Decoder().decodeBuffer(sSrc);//先用base64解密
            try {
                byte[] original = cipher.doFinal(encrypted1);
                String originalString = new String(original,"utf-8");
                originalString=EncryptionUtils.base64Dcode(originalString);
                return originalString;
            } catch (Exception e) {
                System.out.println(e.toString());
                return null;
            }
        } catch (Exception ex) {
            System.out.println(ex.toString());
            return null;
        }
    }
    /** 功能描述: base64 编码
     *[args]
     * @return: void
     * @Author: 杨春科
     * @Date: 2021-09-13 15:09
     * @Description //TODO
     */
    public static String base64Code(String base64encodedString){
        final Base64.Decoder decoder = Base64.getDecoder();
        final Base64.Encoder encoder = Base64.getEncoder();
        final byte[] textByte;
        try {
            textByte = base64encodedString.getBytes("UTF-8");
            base64encodedString = encoder.encodeToString(textByte);
            System.out.println(base64encodedString);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return base64encodedString;
    }

    /** 功能描述: base64 编码
     *[args]
     * @return: void
     * @Author: 杨春科
     * @Date: 2021-09-13 15:09
     * @Description //TODO
     */
    public static String base64Dcode(String base64encodedString){
        final Base64.Decoder decoder = Base64.getDecoder();
        try {
            final byte[] textByte = base64encodedString.getBytes("UTF-8");
            System.out.println(new String(decoder.decode(textByte), "UTF-8"));
            base64encodedString=new String(decoder.decode(textByte), "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return base64encodedString;
    }

    public static void main(String[] args) throws Exception {
        /*
         * 此处使用AES-128-ECB加密模式，key需要为16位。
         */
        // 需要加密的字串
        String cSrc = "456258621458965321475689523642";
        System.out.println("字符串："+cSrc);
        // 加密
        String enString = EncryptionUtils.Encrypt(cSrc);
        System.out.println("加密后的字串是：" + enString);
        System.out.println(enString.length());
        // 解密
        String DeString = EncryptionUtils.Decrypt(enString);
        System.out.println("解密后的字串是：" + DeString);

    }


}
